package com.epower.bo.epowerwordforday.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.epower.base.BaseBO;
import com.epower.bo.epowersentence.EpowerSentenceService;
import com.epower.bo.epowerwordforday.EpowerWordForDayService;
import com.epower.dao.epowerwordforday.EpowerWordForDayDAO;
import com.epower.dataobject.EpowerSentenceDO;
import com.epower.dataobject.EpowerWordForDayDO;

/***
 * BO层实现类
 * 
 * @author QuickDev
 */
@Scope("singleton")
@Component("epowerWordForDayBO")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EpowerWordForDayServiceImpl extends BaseBO implements EpowerWordForDayService {

	private EpowerWordForDayDAO epowerWordForDayDAO;

	private EpowerSentenceService epowerSentenceBO;

	/***
     * 
     */
	@Override
	public EpowerWordForDayDO getEpowerWordForDayByOption(EpowerWordForDayDO epowerWordForDay) {
		return epowerWordForDayDAO.getEpowerWordForDayByOption(epowerWordForDay);
	}

	/***
     * 
     */
	@Override
	public List<EpowerWordForDayDO> getEpowerWordForDayListByOption(EpowerWordForDayDO epowerWordForDay) {
		return epowerWordForDayDAO.getEpowerWordForDayListByOption(epowerWordForDay);
	}

	@Override
	public List<EpowerWordForDayDO> getEpowerWordForDayListPageByOption(EpowerWordForDayDO epowerWordForDay) {
		int totalItem = getEpowerWordForDayCountByOption(epowerWordForDay);
		epowerWordForDay.setTotalItem(totalItem);
		List<EpowerWordForDayDO> epowerWordForDays = epowerWordForDayDAO.getEpowerWordForDayListByOption(epowerWordForDay);
		for (EpowerWordForDayDO tmp : epowerWordForDays) {
			String content = tmp.getContent();
			if (content != null && content.length() > 50) {
				content = content.substring(0, 50);
			}
			tmp.setContent(content + ".....");

			if (tmp.getId().intValue() == tmp.getGroupid().intValue()) {// 这里经常报空指针，groupid没有值造成
				tmp.setMain(1);// 以后要改为常量表示
			} else {
				tmp.setMain(0);// 以后要改为常量表示
			}
		}
		return epowerWordForDays;
	}

	/***
     * 
     */
	@Override
	public EpowerWordForDayDO getEpowerWordForDayById(Integer id) {
		return epowerWordForDayDAO.getEpowerWordForDayById(id);
	}

	/***
     * 
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
	public boolean deleteEpowerWordForDayById(Integer id) {
		try {
			int num = 0;
			if (epowerWordForDayDAO.deleteEpowerWordForDayById(id)) {
				num = 1;
				epowerSentenceBO.deleteEpowerSentenceBySpelling(spelling);
			}
			return num == 0 ? false : true;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private String spelling;

	/***
     * 
     */
	@Override
	public Integer getEpowerWordForDayCountByOption(EpowerWordForDayDO epowerWordForDay) {
		return epowerWordForDayDAO.getEpowerWordForDayCountByOption(epowerWordForDay);
	}

	/***
     * 
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
	public boolean updateEpowerWordForDayByOption(EpowerWordForDayDO epowerWordForDay, List<String> sentence) {
		try {
			boolean flag = epowerWordForDayDAO.updateEpowerWordForDayByOption(epowerWordForDay);
			List<EpowerSentenceDO> sentences = new ArrayList<EpowerSentenceDO>();
			for (String str : sentence) {
				String[] strs = str.split("##");
				if (strs.length < 3 || strs.length > 3) {
					throw new RuntimeException("sentence is error:" + str);
				}
				EpowerSentenceDO sentencedo = new EpowerSentenceDO();
				sentencedo.setSpelling(epowerWordForDay.getSpelling());
				sentencedo.setSentence(strs[0]);
				sentencedo.setExplains(strs[1]);
				sentencedo.setStatus(1);
				if (null != strs[2] && !strs[2].equals("")) {
					sentencedo.setId(Integer.parseInt(strs[2]));
				}
				sentences.add(sentencedo);
			}
			if (sentences.size() > 0 && flag) {
				epowerSentenceBO.addEpowerSentence(sentences);
			}
			return flag;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/***
     * 
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
	public Integer addEpowerWordForDay(EpowerWordForDayDO epowerWordForDay, List<String> sentence) {
		try {
			epowerWordForDay.setStatus(1);
			Integer id = epowerWordForDayDAO.addEpowerWordForDay(epowerWordForDay);
			if (epowerWordForDay.getMain() == 1 && id > 0) {// 如果是主词，则更新groupid（新增主词是无法设置groupid）
				EpowerWordForDayDO temp = new EpowerWordForDayDO();
				temp.setId(id);
				temp.setGroupid(id);
				epowerWordForDayDAO.updateEpowerWordForDayByOption(temp);
			}
			List<EpowerSentenceDO> sentences = new ArrayList<EpowerSentenceDO>();
			for (String str : sentence) {
				String[] strs = str.split("##");
				if (strs.length != 3) {
					throw new RuntimeException("sentence is error:" + str);
				}
				EpowerSentenceDO sentencedo = new EpowerSentenceDO();
				sentencedo.setSpelling(epowerWordForDay.getSpelling()); // 主词
				sentencedo.setExplains(strs[1]);
				sentencedo.setSentence(strs[0]);
				sentencedo.setStatus(1);
				sentencedo.setId(Integer.parseInt(strs[2].trim()));
				sentences.add(sentencedo);
			}
			if (sentences.size() > 0 && id > 0) {
				epowerSentenceBO.addEpowerSentence(sentences);
			}
			return id;
		} catch (Exception e) {
			// throw new RuntimeException("service is error");
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<EpowerWordForDayDO> getMainEpowerWordForDayBySpelling(String spelling) {
		return epowerWordForDayDAO.getMainEpowerWordForDayBySpelling(spelling);
	}

	@Override
	public WordForDayResult getEpowerWordForDayByDate(Date date) {
		WordForDayResult result = new WordForDayResult();
		EpowerWordForDayDO epowerWordForDayDO = epowerWordForDayDAO.getEpowerMainWordByDate(date);
		if (epowerWordForDayDO != null) {
			List<EpowerWordForDayDO> list = epowerWordForDayDAO.queryEpowerMainWordByGroupid(epowerWordForDayDO.getGroupid());
			if (list != null) {
				for (EpowerWordForDayDO info : list) {
					info.setEpowerSentenceBO(epowerSentenceBO);
				}
			}
			epowerWordForDayDO.setEpowerSentenceBO(epowerSentenceBO);
			result.setList(list);
			result.setMain(epowerWordForDayDO);
		}
		return result;
	}

	/***
	 * @getter setter
	 */
	public EpowerWordForDayDAO getEpowerWordForDayDAO() {
		return epowerWordForDayDAO;
	}

	@Resource
	public void setEpowerWordForDayDAO(EpowerWordForDayDAO epowerWordForDayDAO) {
		this.epowerWordForDayDAO = epowerWordForDayDAO;
	}

	public EpowerSentenceService getEpowerSentenceBO() {
		return epowerSentenceBO;
	}

	@Resource
	public void setEpowerSentenceBO(EpowerSentenceService epowerSentenceBO) {
		this.epowerSentenceBO = epowerSentenceBO;
	}

	public class WordForDayResult {
		private EpowerWordForDayDO main;
		private List<EpowerWordForDayDO> list;

		public EpowerWordForDayDO getMain() {
			return main;
		}

		public void setMain(EpowerWordForDayDO main) {
			this.main = main;
		}

		public List<EpowerWordForDayDO> getList() {
			return list;
		}

		public void setList(List<EpowerWordForDayDO> list) {
			this.list = list;
		}
	}
}
